﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using System.Windows.Forms;

namespace hpm
{
    class clsPhongBan
    {
        string _maPhong, _maKhoa, _tenPhong, _diengiai;

        public string MaPhong
        {
            get { return this._maPhong; }
            set { _maPhong = value; }
        }

        public string MaKhoa
        {
            get { return _maKhoa; }
            set { _maKhoa = value; }
        }

        public string TenPhong
        {
            get { return _tenPhong ;}
            set { _tenPhong = value; }
        }

        public string DienGiai
        {
            get { return _diengiai; }
            set { _diengiai = value; }
        }

        public clsPhongBan()
        {
            this._maPhong = "";
            this._maKhoa = "";
            this.DienGiai = "";
        }

        public clsPhongBan(string maPhong, string maKhoa, string diengiai)
        {
            this._maPhong = maPhong;
            this._maKhoa = maKhoa;
            this._diengiai = diengiai;
        }

        DAL da = new DAL();
        DataTable dtbPhongBan = null;
        SqlCommand sqlcom = null;
        SqlDataAdapter sqlDataAdapter = null;

        public DataTable GetAll_PhongBan()
        {
            da.ketnoiServer();
            dtbPhongBan = new DataTable();
            this.sqlcom = new SqlCommand();
            this.sqlcom.CommandText = "Select * from DMPhongBan";
            this.sqlcom.Connection = da.con;
            this.sqlcom.CommandType = CommandType.Text;
            this.sqlDataAdapter = new SqlDataAdapter(this.sqlcom);
            this.sqlDataAdapter.Fill(dtbPhongBan);
            //da.da_closeConnect();
            return dtbPhongBan;
        }


        public DataTable Get_PhongBan(string maKhoa = "")
        {
            da.ketnoiServer();
            dtbPhongBan = new DataTable();
            this.sqlcom = new SqlCommand();
            this.sqlcom.CommandText = "Select * from DMPhongBan Where MaKhoa = '"+ maKhoa +"'";
            this.sqlcom.Connection = da.con;
            this.sqlcom.CommandType = CommandType.Text;
            this.sqlDataAdapter = new SqlDataAdapter(this.sqlcom);
            this.sqlDataAdapter.Fill(dtbPhongBan);
            //da.da_closeConnect();
            return dtbPhongBan;
        }


        public void CapNhatPhongBan()
        {
            try
            {
                SqlCommandBuilder cb = new SqlCommandBuilder(sqlDataAdapter);
                sqlDataAdapter.Update(dtbPhongBan);
            }
            catch (SqlException e)
            {
                MessageBox.Show("Không thể thêm dữ liệu");
            }
        }
    }
}
